<template>
    <div class="admin-page">
        <div class="page-header">
            <div style="display: flex;justify-content: space-between">
                <div style="display: flex; flex-wrap: wrap; align-items: center; gap: 15px; margin-bottom: 15px;">
                    <div style="display: flex; align-items: center;" class="search-item">
                        <span class="search-label">手机号:</span>
                        <el-input
                            v-model="searchForm.search"
                            size="small"
                            style="width: 130px;"
                            placeholder="请输入手机号"
                        />
                    </div>
                    <div style="display: flex; align-items: center;" class="search-item">
                        <span class="search-label">修改类型:</span>
                        <el-select v-model="searchForm.type" size="small" style="width: 120px;" placeholder="全部">
                            <el-option label="前台用户修改" value="1"></el-option>
                            <el-option label="后台修改" value="2"></el-option>
                        </el-select>
                    </div>
                    <div style="display: flex; align-items: center;" class="search-item">
                        <span class="search-label">日期:</span>
                        <el-date-picker
                            size="small"
                            v-model="searchForm.start_time"
                            type="date"
                            style="width: 150px;"
                            placeholder="开始日期">
                        </el-date-picker>
                        <span style="margin: 0 5px;">至</span>
                        <el-date-picker
                            size="small"
                            style="width: 150px;"
                            v-model="searchForm.end_time"
                            type="date"
                            placeholder="结束日期">
                        </el-date-picker>
                    </div>
                </div>
                <div style="display: flex; align-items: center; gap: 15px; margin-bottom: 15px;">
                    <el-button type="primary" size="small" @click="handleSearch">搜索</el-button>
                    <el-button type="success" size="small" style="margin-left: 10px;" @click="handleReset">重置</el-button>
                </div>
            </div>

            <el-table :data="tableData" height="560" border style="width: 100%;overflow-y: auto"
                      :header-cell-style="{background:'#FAFAFA',color:'#666', fontSize:'13px'}" size="small">
                <el-table-column prop="uid" label="用户ID" width="100" align="center" />
                <el-table-column prop="old_tel" label="原手机号" width="120" align="center" />
                <el-table-column prop="new_tel" label="新手机号" width="120" align="center" />
                <el-table-column prop="update_time" label="变更时间" width="160" align="center" />
                <el-table-column prop="type" label="变更类型" width="160" align="center">
                    <template slot-scope="scope">
                        {{ scope.row.type === 1 ? "前台用户修改 " : '后台修改' }}
                    </template>
                </el-table-column>

                <el-table-column prop="type" label="操作人"  align="center">
                    <template slot-scope="scope">
                        {{ scope.row.aname || "用户修改" }}
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-size="pageSize"
                layout="total, prev, pager, next"
                :total="total"
            />
        </div>
    </div>
</template>
<script>
export default {
  data() {
    return {
      searchForm: {
          start_time: '',
          end_time: '',
        search: '',
          type:""
      },
      tableData: [],
      currentPage: 1,
      pageSize: 15,
      total: 0,
        searchParams:{}
    }
  },
  created() {
    this.getKuaidi()
  },
  methods: {
      getKuaidi(params={}) {
          const mergedParams = {
              ...params,  // 先展开传入的参数
              page: params.page || this.currentPage,  // 优先使用传入的页码
              limit: 15  // 固定每页显示数量
          };
          this.$http.get('/admin/UserTelRecord/lists',{params:mergedParams}).then(result => {
              // 假设result.data就是你获取到的原始对象数据
              this.tableData  = result.data.data;
              this.total = result.data.total || 0; // Add total count
          });
      },
    handleSearch() {
        const formatDate = (date) => {
            if (!date) return '';
            const d = new Date(date);
            const year = d.getFullYear();
            const month = String(d.getMonth() + 1).padStart(2, '0');
            const day = String(d.getDate()).padStart(2, '0');
            const hours = String(d.getHours()).padStart(2, '0');
            const minutes = String(d.getMinutes()).padStart(2, '0');
            const seconds = String(d.getSeconds()).padStart(2, '0');
            const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
            const timestamp = new Date(formattedDate).getTime();
            return Math.floor(timestamp / 1000); // 转换为秒级时间戳
        };

        this.searchParams = {
            ...this.searchForm,
            start_time: formatDate(this.searchForm.start_time),
            end_time: formatDate(this.searchForm.end_time)
        };
        // Remove empty string properties from searchParams before sending
        Object.keys(this.searchParams).forEach(key => {
            if (this.searchParams[key] === '') {
                delete this.searchParams[key];
            }
        });
        this.currentPage = 1;
        this.getKuaidi(this.searchParams);
    },
    handleReset() {
      this.searchForm = {

      }
      this.getKuaidi()
    },
    handleSizeChange(val) {
        this.pageSize = val;
        this.currentPage = 1;
        this.getKuaidi({...this.searchParams, page: this.currentPage, limit: val});
    },
    handleCurrentChange(val) {
        this.currentPage = val;
        this.getKuaidi({...this.searchParams, page: val, limit: this.pageSize});
    }
  }
}
</script>
<style scoped lang="less">
.admin-page {
  padding: 20px;

  .search-label {
    margin-right: 10px;
    color: #606266;
  }

  .el-table {
    margin-top: 20px;
  }

  .el-pagination {
    margin-top: 20px;
    text-align: right;
  }
}
</style>
